#!/usr/bin/perl -I /opt/hp/nv/lib/perl
#

use 5.008000;
use strict;
use warnings;
use Data::Dumper;

use instance ':all';
use bright ':all';

my $instance_conf = '/opt/hp/nv/lib/instance.conf';
my $test_instance_conf = 'instance_test.conf';
my $instance_id = 'instance01';
my $user_id = 'sqdev1';
my $racknum = 1;
my $node = 'n011';
my $app = 'NEOVIEW';
my $sqconfig = '/opt/hp/nv/instance/sqconfig_Test_squserN';

my @SUBS = (
    ['set_instance_conf_fileloc', $instance_conf],
    ['get_inst_config_type'],
    ['get_cluster_IP'],
    ['get_instance_id_list'],
    ['get_instance_user_list'],
    ['get_instance_user_list', $instance_id],
    ['get_instance_id', $user_id],
    ['get_instance_sqconfig', $user_id],
    ['get_instance_usergroup', $user_id],
    ['get_instance_usertype', $user_id],
    ['get_instance_storagetype', $user_id],
    ['get_instance_system_build', $user_id],
    ['get_instance_noderange', $user_id],
    ['get_instance_nodelist', $user_id],
    ['get_instance_racklist', $user_id],
    ['get_instance_floatIP_flag', $user_id],
    ['get_instance_ext_floatIP', $user_id],
    ['get_instance_int_floatIP', $user_id],
    ['get_instance_bdrname', $user_id],
    ['get_first_nodes', $user_id],
    ['get_cluster_name'],
    ['get_nodes_in_rack', $racknum],
    ['get_headnodes'],
    ['get_application_nodes', $app],
    ['get_node_type', $node],
    ['get_node_processor_info', $node],
    ['get_node_rack', $node],
    ['get_msas_in_rack', $racknum],
    ['get_msatypes_filename'],
    ['get_image_names'],
    ['get_categories'],
    ['set_instance_sqconfig', $user_id, $sqconfig],
    ['get_instance_conf_ref'],
    ['set_instance_conf']
);

sub testSub
{
   my $sub = shift;
   my @args = @_;

   if ($sub eq 'set_instance_conf') {
      set_instance_conf_fileloc($test_instance_conf, instance::WRITE);
   }

   no strict "refs";
   my $ret = &{$sub}(@args);
   return $ret;
}

my %SubsToTest = map {$_ => 0} @instance::EXPORT_OK;

print "Begin instance.pm test...\n";
my $gv_instance_conf;

for my $subref (@SUBS) {

    push @{$subref}, $gv_instance_conf if ($subref->[0] eq "set_instance_conf");
    my $ret = testSub(@{$subref});

    $gv_instance_conf = $ret if ($subref->[0] eq "get_instance_conf_ref");

    $SubsToTest{$subref->[0]} = 1;
    print "$subref->[0]";
    shift @{$subref};
    print "(" . join(',', @{$subref}) . "):\t" . Dumper($ret);

}

print "\nSubroutines missing test:\n";
for my $sub (keys %SubsToTest) {
    print "\t$sub\n" if (! $SubsToTest{$sub});
}

@SUBS = (
    ['cm_AddExcludeList', 'edge', 'excludelistupdate', '/zzz/aaa'],
    ['cm_RemoveExcludeList', 'edge', 'excludelistupdate', '/zzz/aaa'],
    ['cm_ImageUpdate', 'n001,n002,n003'],
    ['cm_UpdateProvisioners'],
    ['cm_GetClusterName'],
    ['cm_IsNodeUp', $node],
    ['cm_GetNodeGroup'],
    ['cm_GetVersion'],
    ['cm_IsValidUser', $user_id],
    ['cm_Check'],
);
%SubsToTest = map {$_ => 0} @bright::EXPORT_OK;

print "Begin bright.pm test...\n";
for my $subref (@SUBS) {
   my $ret = testSub(@{$subref});
   $SubsToTest{$subref->[0]} = 1;

   print "$subref->[0]";
   shift @{$subref};
   print "(" . join(',', @{$subref}) . "):\t" . Dumper($ret);
}

print "\nSubroutines missing test:\n";
for my $sub (keys %SubsToTest) {
    print "\t$sub\n" if (! $SubsToTest{$sub});
}
